package file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) throws IOException {
        String file = "/Users/guopanbo/Documents/study/java/file/test.txt";
        FileChannel writeChannel = new FileOutputStream(file).getChannel();
        writeChannel.write(ByteBuffer.wrap("Hello, World!".getBytes()));

        FileChannel readChannel = new FileInputStream(file).getChannel();
        System.out.println(readChannel.size());
        ByteBuffer buffer = ByteBuffer.allocate((int) readChannel.size());
        readChannel.read(buffer);
        buffer.flip();
        while(buffer.hasRemaining())
            System.out.printf("%c", buffer.get());

        System.out.println();
        System.out.println(Arrays.toString(buffer.array()));

        readChannel.close();
        writeChannel.close();
    }
}
